spring-cloud服务注册与服务发现
1.服务注册
项目名称:recommend
pom配置
<dependency> <groupId>recommend</groupId> <artifactId>recommend-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
api的接口包中就一个接口(recommend-api Moudle)
@FeignClient(name = "recommend-service") public interface PtjobRecommender { @GetMapping("/recommend/user") List<Long> recommend2One(String userId); }
第二个moudle里包括服务的具体内容和项目的启动(recommend-services Moudle)
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; // // //@EnableDiscoveryClient @EnableEurekaClient //@EnableFeignClients @EnableConfigurationProperties @SpringBootApplication(scanBasePackages = {"campus.recommend.service"}) public class RecommendApplication { public static void main(String[] args) { SpringApplication.run(RecommendApplication.class, args); } } @RestController @RequestMapping(value = "/recommend") public class RecommendService implements Recommender { @Override @RequestMapping(value = "/user",method = RequestMethod.GET) public List<Long> recommend2One(@RequestParam String id) { return Arrays.asList(1L,2L,3L); } }
yml配置文件
spring: application: name: recommend-service profiles: active: dev eureka: client: registry-fetch-interval-seconds: 10 #设置拉取服务注册信息时间,默认60s instance: instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} ip-address: ${spring.cloud.client.ip-address} prefer-ip-address: true health-check-url-path: /actuator/health lease-renewal-interval-in-seconds: 15 #指定续约更新频率,默认是30s lease-expiration-duration-in-seconds: 45 #设置过期剔除时间,默认90s
启动项目
2.服务发现
启动第二个项目web-demo
pom配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>campus-recommend</groupId> <artifactId>campus-recommend-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.1.3.RELEASE</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>9.5.1</version> </dependency>
2.java文件
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @EnableFeignClients(basePackages = "campus.recommend.api") @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class); } }
@RestController
@RequestMapping(value="/api")
public class Hello implements HelloWorld {
@Resource
private Recommender recommender;
@GetMapping("/o")
public String sayHello() {
return "hello" + recommender.recommend2One("");
}
}
3.yaml配置
server: port:8082 spring: application: name: web-demo eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka
feign默认使用ribbon作负载均衡,其中默认规则是轮询,如果要修改规则可通过定义配置类
@RibbonClient(name="recommend-services", configuration=LbConfig.class) public class LbConfig { @Bean public IRule getRule(){ return new RondomRule(); } }
浙公网安备 33010602011771号