公司一直使用的都是dubbo,很少使用springcloud,自学下
安装nacos
nacos即是配置中心也是注册中心,安装参考nacas大致如下
- 修改conf/application.properties文件,nacos支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码。不知道怎么安装mysql参考安装mysql
- sh startup.sh -m standalone 启动
1.构建provider
整体是比较简单的,大家照着下面的配置直接贴代码就行。注意x替换为自己的ip或者包名
pom.xml
<properties>
<java.version>1.8</java.version>
<jackson.version>2.12.4</jackson.version>
<project.version>1.0.0</project.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-source.version>3.2.1</maven-source.version>
<lombok.version>1.18.12</lombok.version>
<spring.boot.version>2.3.2.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<!-- 一般只要在项目的root下的pom.xml中加上上面那些配置 而只需要保留下面的-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
@SpringBootApplication
/**这个注解非常重要**/
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}
application.properties
server.port=8082
spring.application.name=demo-provider
server.servlet.context-path:/provider
spring.cloud.nacos.discovery.server-addr=你的nacosIp地址:8848
management.endpoints.web.exposure.include=*
2.构建consumer的配置
启动类配置
@SpringBootApplication
@MapperScan("com.xxx.demo.dao.mapper")
@ComponentScan(basePackages = {"com.xxx.demo"})
@RestController
@Slf4j
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.xxx.demo.service.feign"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
上图中@EnableDiscoveryClient @EnableFeignClients 这两个注解注意
申明feign的调用
注意这边的要跟provider写的一摸一样,当人也可以把provider的抽象出来提取为api接口,在消费者这边继承。
@FeignClient(name = "demo-provider", path = "/provider")
public interface ProviderClient {
@GetMapping("/echo/{string}")
String hello(@PathVariable String string);
}
pom的配置
与consumer一样
服务端controller
能够调用成功
public class FeignController {
@Resource
private ProviderClient providerClient;
/**
* openfeign示例
* @param string 请求入参
* @return 返回参数
*/
@RequestMapping(value = "/consumer/{string}",method = RequestMethod.GET)
public String consume(@PathVariable String string){
return providerClient.hello(string);
}
}
消费者application.properties
# 新增如下配置 替换为自己的nacos地址
spring.cloud.nacos.discovery.server-addr=x.x.x.x:8848
management.endpoints.web.exposure.include=*