1.Eureka Server
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
security:#认证 basic: enabled: true user: name: user password: password123 server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://user:password123@localhost:8761/eureka/
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
2.Eureka Client
@SpringBootApplication @EnableEurekaClient public class MicroserviceSimpleProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args); } }
@RestController public class UserController { @Autowired private UserRepository userRepository; @Autowired private EurekaClient eurekaClient; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/simple/{id}") public User findById(@PathVariable Long id) { return this.userRepository.findOne(id); } @GetMapping("/eureka-instance")//需要配置actual public String serviceUrl(){ InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false); return instance.getHomePageUrl(); } @GetMapping("/eureka-info")//需要配置actual public ServiceInstance showInfo(){ ServiceInstance localServiceInstance=this.discoveryClient.getLocalServiceInstance(); return localServiceInstance; } }
server: port: 7900 spring: jpa:#jpa generate-ddl: false show-sql: true hibernate: ddl-auto: none datasource:#h2 platform: h2 schema: classpath:schema.sql data: classpath:data.sql application: name: user logging:#log level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.itmuch: DEBUG eureka:#eureka client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eureka/ instance: prefer-ip-address: true #是否使用ip instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}#自定义instance-id
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cc.cloud</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>user</module> <module>eureka</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR4</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> </project>