Step 1 Build the base Eureka-client project
- Build a project that acts as a Eureka-client
<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.example</groupId>
<artifactId>eureka-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- Create the main-entry for the app
Content of
src\main\java\app\EurekaClientApp.java
package app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class EurekaClientApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApp.class, args);
}
}
- Prepare
application.yml
to configure the client
Content of
application.yml
spring:
application:
name: eureka-client
server:
port: 8766
eureka:
client:
register-with-eureka: true
serviceUrl:
defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
Step 2 Add dependency to access MySQL through JPA
- Add the dependency in the
pom.xml
<!-- JPA Data -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- Update the
application.yml
file, to define the parameters to MySQL
Content of
application.yml
spring:
application:
name: eureka-client
jpa:
database: MYSQL
show-sql: true
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://127.0.0.1:3306/xiaoyouhui
username: root
password: password123
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8766
eureka:
client:
register-with-eureka: true
serviceUrl:
defaultZone: http://aaa.bbb.ccc.ddd:8761/eureka/
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
- Create the model-class
package app.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "member")
public class Member{
//fields
//getters & setters
}
- Create the repository
package app.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import domain.Member;
@Repository
public interface MemberRepository extends JpaRepository<Member, String>{
}
Step 3 Add a controller to handle request
The controller-class should under the package of the main-entry. Since Spring-boot will scan from the root-package.
Content of
src\main\java\app\controller\MainController.java
package app.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import domain.Member;
import repository.MemberRepository;
@RestController
public class MainController {
@Autowired
private MemberRepository memberRepository;
@RequestMapping("/greeting")
public Optional<Member> greeting(@RequestParam(value="id", defaultValue="na") String id) {
return memberRepository.findById(id);
}
@RequestMapping("/greetings")
public List<Member> greetings() {
List<Member> list = memberRepository.findAll();
System.out.println(list.size());
return list;
}
@GetMapping(path="/user/{id}")
public String findById(@PathVariable Long id) {
return "hello";
}
@RequestMapping("/test")
public @ResponseBody String testm() {
return "test";
}
}
Step 4 Configure to open some Actuator end-point
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
shutdown:
enabled: false