springcloud微服务实战 学习笔记二 服务提供者
2017-08-04 本文已影响26人
maylor_zhu
-
添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
配置文件
spring.application.name=eureka-client server.port=2222 eureka.instance.hostname=localhost #服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
-
Application.java
@EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
@EnableDiscoveryClient开启了服务发现
-
controller
@RestController public class DemoController { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired DiscoveryClient discoveryClient; @GetMapping("/hello") public String hello(){ logger.info("service:"+ discoveryClient.getServices()); return "hello world"; } }